﻿'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class IcqUsageReportService
    Inherits ContextService

    Implements Interfaces.IUsageReportService

    Public Sub New(ByVal context As Interfaces.IContext)
        MyBase.New(context)

        Dim connector As IcqConnector = TryCast(context.GetService(Of Interfaces.IConnector)(), IcqConnector)

        If connector Is Nothing Then Throw New InvalidCastException("Context Connector Service must be of Type IcqConnector")

        connector.RegisterSnacHandler(&HB, &H2, New Action(Of DataTypes.Snac0B02)(AddressOf AnalyseSnac0B02))
        connector.RegisterSnacHandler(&HB, &H4, New Action(Of DataTypes.Snac0B04)(AddressOf AnalyseSnac0B04))
    End Sub

    Public Event MinimumUsageReportIntervallReceived(ByVal sender As Object, ByVal e As Interfaces.IntervallReceivedEventArgs) Implements Interfaces.IUsageReportService.MinimumUsageReportIntervallReceived

    Private Sub AnalyseSnac0B02(ByVal data As DataTypes.Snac0B02)
        Try
            Core.Kernel.Logger.Log("IcqUsageReportService", _
                                   TraceEventType.Information, _
                                   "Minimum report intervall: {0}", _
                                   data.MinimumReportIntervall)

            RaiseEvent MinimumUsageReportIntervallReceived(Me, _
                                                           New Interfaces.IntervallReceivedEventArgs(data.MinimumReportIntervall))
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub AnalyseSnac0B04(ByVal data As DataTypes.Snac0B04)
        Try
            Core.Kernel.Logger.Log("IcqUsageReportService", _
                                   TraceEventType.Information, _
                                   "Usage report accepted.")

            RaiseEvent UsageReportAccepted(Me, EventArgs.Empty)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Public Sub SendUsageReport() Implements Interfaces.IUsageReportService.SendUsageReport
        Dim dataOut As DataTypes.Snac0B03

        dataOut = New DataTypes.Snac0B03

        dataOut.TlvUsageReport.ScreenName = Context.Identity.Identifier

        ' TODO: IcqUsageReportService
        ' Implement proper plattform description
        dataOut.TlvUsageReport.OperatingSystem = Environment.OSVersion.Platform.ToString
        dataOut.TlvUsageReport.OperatingSystemVersion = Environment.OSVersion.Version
        dataOut.TlvUsageReport.ProcessorType = "Unknown"
        dataOut.TlvUsageReport.WinsockDllDescription = "Unknown"
        dataOut.TlvUsageReport.WinsockDllVersion = New Version(0, 0, 0, 0)

        Dim transfer As IIcqDataTranferService = DirectCast(Context.GetService(Of Interfaces.IConnector)(), IIcqDataTranferService)
        transfer.Send(dataOut)
    End Sub

    Public Event UsageReportAccepted(ByVal sender As Object, ByVal e As System.EventArgs) Implements Interfaces.IUsageReportService.UsageReportAccepted
End Class
